package towerDefense.screens;

import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import towerDefense.TowerDefense;
import util.BufferedImageModifier;
import com.golden.gamedev.GameEngine;
import com.golden.gamedev.object.Background;
import com.golden.gamedev.object.Sprite;
import com.golden.gamedev.object.SpriteGroup;
import com.golden.gamedev.object.Timer;
import com.golden.gamedev.object.background.ImageBackground;


public class MainMenu extends MenuScreen
{

    //Background qualities
    Background mainMenuBack;
    BufferedImage mainBG, title;

    //menu toggle
    private static final int TITLE = 0, NGMENU = 1;
    int menuPhase;

    //menu images
    public static final String MENU_IMAGE_DIR = "src/images/menu/main/";
    Sprite newgame, newgameselect, exit, exitselect, titlesprite;
    SpriteGroup MENU_GROUP;
    Timer menuDelay = new Timer(500);


    //Constructor
    public MainMenu (GameEngine ge, File bg, File title)
    {
        super(ge);
    }


    @Override
    public void initResources ()
    {
        MENU_GROUP = new SpriteGroup("Menu Group");
        try
        {
            mainBG = ImageIO.read(new File(MENU_IMAGE_DIR + "mainmenu.jpg"));
            title = ImageIO.read(new File(MENU_IMAGE_DIR + "title.png"));
            title =
                BufferedImageModifier.getScaledInstance(title,
                                                        (int) (getHeight() /
                                                               (double) title.getHeight() * title.getWidth()),
                                                        getHeight());

            newgame =
                new Sprite(createMenuItem(MENU_IMAGE_DIR + "newgame.png", 5));
            initializeMenuItem(newgame,
                               this.getWidth() / 2 - newgame.getWidth() / 2,
                               this.getHeight() / 2 - newgame.getHeight());

            newgameselect =
                new Sprite(createMenuItem(MENU_IMAGE_DIR + "newgameselect.png",
                                          5));
            initializeMenuItem(newgameselect,
                               this.getWidth() / 2 - newgameselect.getWidth() /
                                       2,
                               this.getHeight() / 2 - newgameselect.getHeight());

            exit = new Sprite(createMenuItem(MENU_IMAGE_DIR + "exit.png", 5));
            initializeMenuItem(exit,
                               this.getWidth() / 2 - exit.getWidth() / 2,
                               this.getHeight() / 2);

            exitselect =
                new Sprite(createMenuItem(MENU_IMAGE_DIR + "exitselect.png", 5));
            initializeMenuItem(exitselect,
                               this.getWidth() / 2 - exitselect.getWidth() / 2,
                               this.getHeight() / 2);

        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        if (mainBG.getWidth() / mainBG.getHeight() > this.getWidth() /
                                                     this.getHeight())
        {
            mainBG =
                BufferedImageModifier.getCenterSection(BufferedImageModifier.getScaledInstance(mainBG,
                                                                                               (int) Math.ceil((getHeight() *
                                                                                                                (double) mainBG.getWidth() / mainBG.getHeight())),
                                                                                               getHeight()),
                                                       getWidth(),
                                                       getHeight());
        }
        else mainBG =
            BufferedImageModifier.getScaledInstance(mainBG,
                                                    getWidth(),
                                                    getHeight());

        titlesprite =
            new Sprite(title, (this.getWidth() - title.getTileWidth()) / 2, 0);
        mainMenuBack =
            new ImageBackground(mainBG, this.getWidth(), this.getHeight());
        menuPhase = TITLE;

    }


    /**
     * @param j
     * @param i
     * @param string
     * @param option
     * @throws IOException
     */
    private void initializeMenuItem (Sprite option, int i, int j)
        throws IOException
    {
        option.setLocation(i, j);
        option.setActive(false);
        MENU_GROUP.add(option);
    }


    @Override
    public void render (Graphics2D g)
    {
        mainMenuBack.render(g);
        if (menuPhase == TITLE) titlesprite.render(g);
        MENU_GROUP.render(g);

    }


    private void renderAroundPoint (Graphics2D g,
                                    int x,
                                    int y,
                                    BufferedImage option)
    {
        g.drawImage(option, x - option.getWidth() / 2, y - option.getHeight() /
                                                       2, null);

    }


    @Override
    public void update (long elapsedTime)
    {

        if (menuPhase == NGMENU && this.checkPosMouse(newgame, false))
        {
            newgameselect.setActive(true);
            newgame.setActive(false);
            if (this.click())
            {
                parent.nextGameID = TowerDefense.ELEMENT_SELECT_MENU;
                finish();
            }
        }
        else if (menuPhase == NGMENU)
        {
            newgameselect.setActive(false);
            newgame.setActive(true);
        }

        if (menuPhase == NGMENU && this.checkPosMouse(exit, false))
        {
            exitselect.setActive(true);
            exit.setActive(false);
            if (this.click())
            {
                finish();
            }
        }
        else if (menuPhase == NGMENU)
        {
            exitselect.setActive(false);
            exit.setActive(true);
        }

        if (menuPhase == TITLE && this.click())
        {
            titlesprite.setActive(false);
            menuPhase = NGMENU;
        }

        if (bsInput.getKeyPressed() == KeyEvent.VK_ESCAPE) finish();

    }

}
